<?php

namespace backend\controllers;
use common\helps\redisHelper;
use common\helps\tools;
use common\models\CustMaster;
use common\models\SysCity;
use frontend\models\ForgetPasswordForm;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\AdminController;

class CustController extends AdminController{
    public $layout = true;

    //业务关键字
    public $service = 'Cust';

    public function actionIndex(){
		//获取地址信息
		//加载数据
		$filename = __DIR__ . '/../../backend/config/sys_city.php';
		$sys_city = file_get_contents($filename);
		return $this->render('index',['sysCity'=>$sys_city]);
	}


	public function actionList(){

		//接收数据
		$data = Yii::$app->request->post();
		//$condition	= tools::dd($data);
		$condition[] = 'and';

		//增加查询条件
		if($data['mobile']){
			$condition[]=['mobile'=>$data['mobile']];
		}

		if($data['member_name']){
			$condition[]= ['like','username',$data['member_name']];
		}

		if($data['member_no']){
			$condition[]=['member_no'=>$data['member_no']];
		}

		//日期条件
		if(!empty($data['reservationtime']) || $data['reservationtime'] !=''){
			$dateval = explode (' - ',  $data['reservationtime']);
			$condition[] = ['between', 'create_time', $dateval[0],$dateval[1]];
		}

		//获取总条数
		$query = CustMaster::find()->where($condition);

		$count = $query->count();

		$sort_string = empty($data['columns'][$data['order'][0]['column']]['data']) ? 'id' : $data['columns'][$data['order'][0]['column']]['data'];
		$list = $query
				->offset($data['start'])
				->limit($data['length'])
				->orderBy(
					[$sort_string=>$data['order'][0]['dir'] == 'asc' ? SORT_ASC : SORT_DESC]
				)
				->asArray()
				->all();
		$result['draw'] = intval($data['draw']);
		$result['recordsTotal'] = intval($count);
		$result['recordsFiltered'] = intval($count);
		$result['data'] = $list;
		return \yii\helpers\Json::encode($result);
	}

	/**
	 * @param密码重置短信
	 */
	public function actionResetSmsPassword(){
		try{
			$mobile		= Yii::$app->request->post('mobile','');

			//查询对应的数据
			$custMaster		= new CustMaster();
			$custMasterInfo	= $custMaster->checkMobileIsRegister($mobile,1);

			if(!$custMasterInfo){
				throw new \Exception('数据不存在');
			}

			//生成随机密码
			$string	= 'jd'.tools::roundCode(4);

			//发送短信
			tools::sendMobileCode($custMasterInfo->mobile,$string,'sms_crcode');

			//重置密码
			$custMasterInfo->pwd	= tools::md5pwd($string);
			if(!$custMasterInfo->save()){
				throw new \Exception('更新失败');
			}
			return \yii\helpers\Json::encode(['code'=>200,'data'=>[],'msg'=>'成功']);
		}catch (\Exception $e){
			//add by allen.qiang 20180112 异常处理，日志优化 start
			error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
			//add by allen.qiang 20180112 异常处理，日志优化 end
			return \yii\helpers\Json::encode(['code'=>500,'data'=>[],'val'=>$e->getMessage()]);
		}
	}

	/**
	 * @param密码重置短信
	 */
	public function actionResetPassword(){
		try{
			$model 	= new ForgetPasswordForm();

			//设置场景
			$model->setScenario('backend');
			$model->load(Yii::$app->request->post(),'');

			//验证信息
			if(!$model->validate()){
				throw new \Exception(array_shift($model->getErrors())[0]);
			}

			//处理保存对应数据
			$custModel	= new CustMaster();
			$info = $custModel->checkMobileIsRegister($model->mobile,1);
			if(!$info){
				throw new \Exception('此会员不存在');
			}

			//修改密码
			$info->pwd = tools::md5pwd($model->pwd);
			if(!$info->save()){
				throw new \Exception('更新密码失败');
			}

			return \yii\helpers\Json::encode(['code'=>200,'val'=>'']);

		}catch (\Exception $e){
			//add by allen.qiang 20180112 异常处理，日志优化 start
			error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
			//add by allen.qiang 20180112 异常处理，日志优化 end
			return \yii\helpers\Json::encode(['val'=>$e->getMessage(),'code'=>'500']);
		}
	}

	/**
	 * @param 更新用户数据
	 */
	public function actionUpdateCustMaster(){
		try{
			//查询对应的数据
			$member_no	= Yii::$app->request->post('member_no','');
			$custMaster		= CustMaster::getMastertMobile($member_no);

			$custMaster->load(Yii::$app->request->post(),'');
			$custMaster->province_code = Yii::$app->request->post('province_id');
			$custMaster->city_code = Yii::$app->request->post('city_id');
			$custMaster->district_code = Yii::$app->request->post('area_id');
			//处理地址名称
			if($custMaster->district_code){
				$custMaster->district_name 	= SysCity::getName($custMaster->district_code)->name;
			}

			if($custMaster->province_code){
				$custMaster->province_name 	= SysCity::getName($custMaster->province_code)->name;
			}

			if($custMaster->city_code){
				$custMaster->city_name 		= SysCity::getName($custMaster->city_code)->name;
			}
			if(!$custMaster->validate()){
				throw new \Exception('参数错误');
			}

			//修改线下数据
			$soap	= new \common\helps\Soap();
			$soap->load(ArrayHelper::toArray($custMaster),'');
			if(!$soap->modify()){
				throw new \Exception(
					$GLOBALS['WEB_EXCEPTION_DATA'][WEB_EXCEPTION_NUM_1022],
					WEB_EXCEPTION_NUM_1022
				); //修改线下数据失败
			}

			//更新线上数据
			if(!$custMaster->save()){
				throw new \Exception(
					$GLOBALS['WEB_EXCEPTION_DATA'][WEB_EXCEPTION_NUM_1016],
					WEB_EXCEPTION_NUM_1016
				);//修改线上数据错误
			}
			//清除用户缓存
			redisHelper::delMastserMobileInfo($custMaster->mobile);
			return \yii\helpers\Json::encode(['code'=>200,'data'=>[],'val'=>'修改成功']);

		}catch (\Exception $e){
			//add by allen.qiang 20180112 异常处理，日志优化 start
			error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
			//add by allen.qiang 20180112 异常处理，日志优化 end
			return \yii\helpers\Json::encode(['code'=>500,'data'=>[],'val'=>$e->getMessage()]);
		}
	}


}